import numpy as np



def new_array(array,rate):  #array是定义好的全1矩阵，rate是矩阵中每个值取0的概率
    '''按照数组模板生成对应的 0-1 矩阵'''
    zeros_num = int(array.size * rate)  #根据0的比率来得到 0的个数
    new_array = np.ones(array.size)  #生成与原来模板相同的矩阵，全为1
    new_array[:zeros_num] = 0  #将一部分换为0
    np.random.shuffle(new_array)  #将0和1的顺序打乱
    re_array = new_array.reshape(array.shape)  #重新定义矩阵的维度，与模板相同
    return re_array


arr = np.ones(25)  # 生成一维数组arr，值默认为1
arr2 = arr.reshape(5, 5)  # 将数组array变形成15*15的矩阵arr2
new_arr = new_array(arr2,rate=0.5)
print(new_arr)

N = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,]



k = -1
def up(i,j):
    if i == -6:
        i = 4
    if new_arr[i][j] == 0:
        N[k] = N[k] + 1
        new_arr[i][j] = 2
        e = i
        f = j-1
        left(e,f)
        g = i
        h = j+1
        right(g,h)
        i = i-1
        up(i,j)
    else:
        N[k] = N[k]


def down(i,j):
    if i == 5 :
        i = -5
    if new_arr[i][j] == 0:
        N[k] = N[k] + 1
        new_arr[i][j] = 2
        e = i
        f = j-1
        left(e,f)
        g = i
        h = j+1
        right(g,h)
        i = i+1
        down(i,j)
    else:
        N[k] = N[k]


def left(i,j):
    if j == -6:
        j = 4
    if new_arr[i][j] == 0:
        N[k] = N[k] + 1
        new_arr[i][j] = 2
        a = i-1
        b = j
        up(a,b)
        c = i+1
        d = j
        down(c,d)
        j = j-1
        left(i,j)
    else:
        N[k] = N[k]


def right(i,j):
    if j == 5:
        j = -5
    if new_arr[i][j] == 0:
        N[k] = N[k] + 1
        new_arr[i][j] = 2
        a = i-1
        b = j
        up(a,b)
        c = i+1
        d = j
        down(c,d)
        j = j+1
        right(i,j)
    else:
        N[k] = N[k]




def Sagiri():
    global k
    for i in range(0,5,1):
        for j in range(0,5,1):
            k = k+1
            if new_arr[i][j] == 0:
                N[k] = N[k] + 1
                new_arr[i][j] = 2
                a = i-1
                b = j
                up(a,b)
                c = i+1
                d = j
                down(c,d)
                e = i
                f = j-1
                left(e,f)
                g = i
                h = j+1
                right(g,h)
            if new_arr[i][j] == 1:
                continue
Sagiri()
for i in range(0,25,5):
    print(N[i],N[i+1],N[i+2],N[i+3],N[i+4])
print(new_arr)


